// components/my-cartBottom/my-cartBottom.js
import { createStoreBindings, storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import {store} from '../../store/store'
import eventBus from '../../eventBus';

Component({
  behaviors:[storeBindingsBehavior],
  storeBindings:{
    store,
    fields:{

    },
    actions:{

    }
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    Totalprice:0,
    goodsNum:0,
    isSelect:false,
    isCheck:false,
    isntDelete:0,
    buttonText1:'去结算',
    buttonText2:'删除',
    deleteTime:0,
    // 明细
    detail:[]
  },
  observers:{
    'detail':function(){
      this.setData({
        Totalprice:0
      })
      for(var i =0;i<this.data.detail.length;i++){
        this.setData({
          Totalprice:this.data.Totalprice+this.data.detail[i].goodsNum*this.data.detail[i].price
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    selectAllGoods(){
      this.setData({
        isSelect:!this.data.isSelect
      })
      eventBus.emit('updateData');
    },
    checkMySelect(){
      var packagea=JSON.stringify(this.data.detail)
      if(!this.data.isntDelete){
        eventBus.emit('detailTransmit',packagea),
        eventBus.emit('totalPriceTransmit',this.data.Totalprice),
        eventBus.emit('changeBackground')
      }
    },
    calculatePrice(){
      console.log('当前detail1111');
      console.log(this.data.detail);
      console.log(this.data.deleteTime);
      if(this.data.deleteTime){
        console.log(1515810);
        eventBus.emit('timeToDelete')
      }else{
        wx.navigateTo({
          url: '/pkgInnerCommodity/pages/settle/settle',
        })
      }

      // 检测是否有地址，方法待定
      // wx.showModal({
      //   title: '提示',
      //   content: '你还没有收货地址哦，赶紧设置一个吧！',
      //   complete: (res) => {
      //     if (res.confirm) {
      //       wx.navigateTo({
      //         url: '/pkgInnerCommodity/pages/innerCommodity/createAddress/createAddress',
      //       })
      //     }
      //   }
      // })
    }
  },
  lifetimes:{
    attached(){
      eventBus.on('changeToEdit',(data)=>{

        this.setData({
          deleteTime:data,
          isntDelete:!this.data.isntDelete,
        })
      }),
      eventBus.on('transmitToBottom',(data)=>{
        console.log('transmitToBottom,启动！');
        var packagea=JSON.parse(data)
        this.setData({
          detail:packagea
        })
        console.log(26849999);
        console.log(this.data.detail);
      }),
      eventBus.on('numEnough',(data)=>{
        if(data===1){
          this.setData({
            isSelect:1
          })
        }else{
          this.setData({
            isSelect:0
          })
        }
      })
    },
    detached(){
      eventBus.off('changeToEdit');
      eventBus.off('transmitToBottom');
      eventBus.off('numEnough')
    }
  }
})
